<?php
session_start();
require '../nette/Nette/loader.php';
require '../db.php';
require '../localization/lang.php';
require '../core/core.sanitizer.php';
require '../core/core.security.php';
require '../core/core.time.php';
require '../core/core.attendance.php';



function getattend($time) {
	$date=new core_time;
	$query="select * from attendance where uid=".$_POST['uid']." AND day=".$time."";
	$result=mysql_query($query);
	$row=mysql_fetch_array($result);
	if ($row['id'] != "") {
		return $date->tohm($row['att']);
		} else {return "0:00";}
	}

function getnote($time) {
	$date=new core_time;
	$query="select * from attendance where uid=".$_POST['uid']." AND day=".$time."";
	$result=mysql_query($query);
	$row=mysql_fetch_array($result);
	if ($row['id'] != "" AND $row['note'] != "") {
		return $row['note'];
		} else {return "";}
	}

$login=new core_security;
if ($login->checklogin()=="OK") {
$time=new core_time;
$loc=new loc;
$timestamp=strtotime($_POST['month']."/1/".$_POST['year']);
$daysnumber=date(t,$timestamp);
$firstday=date(N,$timestamp);
echo "<h1>".$loc->atendancefor2.": ".$loc->month[$_POST['month']]."</h1>";
$uquery="select name from users where id=".$_POST['uid']."";
$result=mysql_query($uquery);
$row=mysql_fetch_array($result);
echo "<h2> User: ".$row['name']."</h2>";


$attendquery="select sum(att) as total from attendance where uid=".$_POST['uid']." AND day >= ".strtotime($_POST['month']."/1/".$_POST['year'])." AND day <= ".strtotime($_POST['month']."/".$daysnumber."/".$_POST['year'])."";
$resultattend=mysql_query($attendquery);
$rowattend=mysql_fetch_array($resultattend);

//echo $attendquery;
echo "<h3>".$loc->totallatendanceformonth.": ".$time->tohm($rowattend['total'])."</h3>";
echo "<table class='monthtable'> <tr>";
for ($i=1;$i<=7;$i++) {
	echo "<th class='monthth'><b>".$loc->weekday[$i]."</b></th>";
	}
echo "</tr>";
echo "<tr>";
for ($i=1;$i<$firstday;$i++) {
	echo "<td class='dayempty'> </td>";
	}

for ($i=1;$i<=$daysnumber;$i++) {
	if (getattend(strtotime($_POST['month']."/".$i."/".$_POST['year'])) != "edit" AND getattend(strtotime($_POST['month']."/".$i."/".$_POST['year'])) != "0:0" AND getattend(strtotime($_POST['month']."/".$i."/".$_POST['year'])) != "0:00") {$class='day_set';}else{$class='day';}
		if (date(N,strtotime($_POST['month']."/".$i."/".$_POST['year'])) == 6 OR date(N,strtotime($_POST['month']."/".$i."/".$_POST['year'])) == 7 AND $class != "day_set") {$class="weekend ".$class;}
	echo "<td class='".$class."'><span class='daynumber'>".$i."</span><br><br><div class='centered'><span class='editattend' id='".$i."'>".getattend(strtotime($_POST['month']."/".$i."/".$_POST['year']))."</span></div><br><span id='".$i."' class='note'>".getnote(strtotime($_POST['month']."/".$i."/".$_POST['year']))."</span><br><span id='computeattend' onClick='getdetails(\"".$_POST['month']."/".$i."/".$_POST['year']."\",\"".$_POST['uid']."\");'>details</span></td>";
	if (date(N,strtotime($_POST['month']."/".$i."/".$_POST['year']))==7 AND $i != $daysnumber) {
		echo "</tr><tr>";
		}
	}
$i--;
$dn=date(N,strtotime($_POST['month']."/".$i."/".$_POST['year']));
if ($dn!=7) {
	
	for ($i=$dn;$i<7;$i++) {
		echo "<td class='dayempty'> </td>";
		}
	}


echo "</tr>";
echo "</table>";



//echo $dn;

}
